package com.s63.ssm.service;

import com.s63.ssm.dao.UserMapper;
import com.s63.ssm.entity.Page;
import com.s63.ssm.entity.SysUser;
import com.s63.ssm.singleton.ConfigManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.List;
import java.util.Map;

//@Scope("prototype")
@Service("userService")
@Transactional(readOnly = true)
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public SysUser getLogin(String account, String password) throws Exception {
        SysUser user = userMapper.getByAccount(account);
        if (user == null) throw new Exception("用户不存在");
        if (!user.getPassword().equals(password)) {
            throw new Exception("密码不正确");
        }
        return user;
    }

    public UserServiceImpl() {
        System.out.println("UserServiceImpl实例被创建了");
    }

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public int count() {
        return userMapper.count();
    }

    @Override
    public List<SysUser> getAll() {
        return userMapper.getAll();
    }

    @Override
    public List<SysUser> getByRealName(String realName) {
        return userMapper.getByRealName(realName);
    }

    @Override
    public List<SysUser> getByRealNameAndRoleId(String realName, Integer roleId) {
        return null;
    }

    @Override
    public void getByRealNameAndRoleIdPage(String realName, Integer roleId, Page<SysUser> page) {
        // 设置总记录数和总页数
        page.setTotal(userMapper.getByRealNameAndRoleIdCount(realName, roleId));
        // 设置页面边界
        if (page.getPageNo() < 1) page.setPageNo(1);
        if (page.getPageNo() > page.getTotalPages()) page.setPageNo(page.getTotalPages());
        // 设置分页集合
        page.setList(userMapper.getByRealNameAndRoleId(realName, roleId, (page.getPageNo() - 1) * page.getPageSize(), page.getPageSize()));
    }

    @Override
    public List<SysUser> getByUser(SysUser user) {
        return userMapper.getByUser(user);
    }

    @Override
    public List<SysUser> getByMap(Map<String, Object> map) {
        return null;
    }

    @Transactional
    @Override
    public int add(SysUser user) {
        return userMapper.add(user);
    }

    @Transactional
    @Override
    public int del(Integer id) {
        // 删除图片
        SysUser user = userMapper.getById(id);
        delFile(user.getIdPicPath());
        delFile(user.getWorkPicPath());

        // 删除数据
        return userMapper.del(id);
    }

    private void delFile(String filename) {
        if (StringUtils.hasText(filename)) {
            String path = ConfigManager.getInstance().getValue("upload.path");
            File file = new File(path, filename);
            if (file.exists()) {
                file.delete();
            }
        }
    }

    @Transactional
    @Override
    public int update(SysUser user) {
        return userMapper.update(user);
    }

    @Override
    public List<SysUser> getByRealNameAndRoleIdWithChoose(String realName, Integer roleId) {
        return null;
    }

    @Override
    public List<SysUser> getByRealNameAndRoleIds(String realName, Integer[] roleIds) {
        return null;
    }

    @Override
    public List<SysUser> getByRealNameAndRoleIdList(String realName, List<Integer> roleIds) {
        return null;
    }

    @Override
    public SysUser getById(Integer id) {
        return userMapper.getById(id);
    }

    @Override
    public SysUser getByAccount(String account) {
        return userMapper.getByAccount(account);
    }
}
